//
//  PayTool.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/9/9.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "PayTool.h"
#import "PayNetwork.h"
#import "UIAlertView+Entension.h"
#import "SZLAccountHelper.h"
#import "WaterRippleWait.h"
#import "SinglePayNetwork.h"
#import "MBProgressHUD+MJ.h"
#import <AlipaySDK/AlipaySDK.h>
#import "SZLCardPayWebViewController.h"
#import "SZLNavigationController.h"
#import "LottieLoading.h"

@interface PayTool()

@property (nonatomic,strong) NSDictionary *parame;
@property (nonatomic,strong) LeftMoneyCallBack leftCallBack;
@property (nonatomic,strong) AlipayCallBack alipayCallBack;
@property (nonatomic,strong) WeixinPayCallBack weixinPayCallBack;
@property (nonatomic,strong) CardPayCallBack cardPayCallBack;

@end

@implementation PayTool

static PayTool *_tool;
+ (instancetype)shareTool
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _tool = [[PayTool alloc]init];
    });
    return _tool;
}
#pragma mark ----------------余额支付-------------------------
- (void)leftMoneyWithParame:(NSDictionary *)parame CallBack:(LeftMoneyCallBack)callBack
{
    self.parame = parame;
    self.leftCallBack = callBack;
    [self leftMoneyPay];
//    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请输入登录密码" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//    alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
//    [alert show];
//    __WEAK
//    [alert handlerClickedButton:^(UIAlertView *alertView, NSInteger btnIndex) {
//        if (btnIndex == 1) {
//            UITextField *secureField = [alertView textFieldAtIndex:0];
//            [weakSelf checkLoginPsw:secureField.text];
//        }
//    }];
}
//验证支付密码
- (void)checkLoginPsw:(NSString *)psw
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"loginPwd"] = psw;
    [self showWaiter];//余额支付显示等待
    [SinglePayNetwork checkPswWithParame:parame response:^(BOOL success, NSString *msg) {
        if (success) {
            [self leftMoneyPay];
        }else{
            [self hideWaiter];
            [MBProgressHUD showError:@"登录密码验证失败"];
        }
    }];
}
//余额支付
- (void)leftMoneyPay
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"payNumber"] = self.parame[@"payNumber"];
    [self showWaiter];
    [SinglePayNetwork balancePayWithParame:parame response:^(BOOL success, NSString *msg) {
        if (success) {
            [self checkPaySuccess];
        }else{
            [self hideWaiter];
            [MBProgressHUD showError:msg];
        }
    }];
}

#pragma mark -------------------支付宝支付--------------------------
- (void)aplipayWithParame:(NSDictionary *)parame callback:(AlipayCallBack)callBack
{
    self.parame = parame;
    NSString *appScheme = @"alipaywanzhuandiqiu";
    self.alipayCallBack = callBack;
    [self showWaiter];
    [PayNetwork alipayWithParame:parame response:^(BOOL success, NSString *msg, NSString *orderString) {
        [self hideWaiter];
        if (success) {
            //下面的没用到
            [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {//wap支付结果回调
                [self checkPaySuccess];
            }];
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}
#pragma mark -------------------微信支付--------------------------
- (void)weixinPayWithParame:(NSDictionary *)parame callback:(AlipayCallBack)callBack
{
    self.parame = parame;
    self.weixinPayCallBack = callBack;
    [self showWaiter];
    [PayNetwork weixinPayWithParame:parame response:^(BOOL success, NSString *msg, PayReq *req) {
        [self hideWaiter];
        if (success) {
            [WXApi sendReq:req];
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}
#pragma mark ---------------微信回调结果--------------------
- (void)onResp:(BaseResp *)resp
{
    if([resp isKindOfClass:[PayResp class]]){
        [self checkPaySuccess];
    }
}
#pragma mark -------------------招商银行卡--------------------------
- (void)cardPayWithParame:(NSDictionary *)parame callback:(CardPayCallBack)callBack
{
    self.parame = parame;
    self.cardPayCallBack = callBack;
    [self showWaiter];
    [PayNetwork cardPayWithParame:parame response:^(BOOL success, NSString *msg, NSString *url) {
        [self hideWaiter];
        if (success) {
            NSLog(@"url---%@",url);
            SZLCardPayWebViewController *webVc = [[SZLCardPayWebViewController alloc]init];
            webVc.webStr = [url stringByReplacingOccurrencesOfString:@"|" withString:@"%7C"];
            SZLNavigationController *navVc = [[SZLNavigationController alloc]initWithRootViewController:webVc];
            UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
            [rootVc presentViewController:navVc animated:YES completion:nil];
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}
#pragma mark --------------------------------
- (void)checkPaySuccess
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"payNumber"] = self.parame[@"payNumber"];
    __WEAK
    [SinglePayNetwork checkPaySuccessWithParame:parame response:^(BOOL success, NSString *msg) {
        [weakSelf hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"支付成功"];
            if (weakSelf.leftCallBack) {
                weakSelf.leftCallBack();
                return ;
            }
            if (weakSelf.alipayCallBack) {
                weakSelf.alipayCallBack();
                return ;
            }
            if (weakSelf.weixinPayCallBack) {
                weakSelf.weixinPayCallBack();
                return ;
            }
            if (weakSelf.cardPayCallBack) {
                weakSelf.cardPayCallBack();
                return ;
            }
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}

- (void)showWaiter
{
//    [WaterRippleWait waterRippleShowBackgroundColor:[UIColor clearColor]];
    [LottieLoading showLoadingInView:nil];
}
- (void)hideWaiter
{
//    [WaterRippleWait hideWaterRipple];
    [LottieLoading hideLoadingInView:nil];
}

@end
